package com.xr.blog.controller;

import com.xr.blog.service.LoginService;
import com.xr.blog.service.SysUserService;
import com.xr.blog.tools.Result;
import com.xr.blog.tools.constant.Code;
import com.xr.blog.tools.security.LoginBody;
import org.apache.commons.codec.DecoderException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.List;

@RestController
@RequestMapping("/squirrelblog")
public class LoginController {
    @Autowired
    LoginService loginService;
    @Autowired
    SysUserService sysUserService;

    @PostMapping(value = "/login",produces = "application/json;charset=UTF-8")
    public Result<String> login(@RequestBody LoginBody loginBody, HttpServletRequest request) throws NoSuchAlgorithmException, InvalidKeySpecException, DecoderException {
        return loginService.login(loginBody);
    }

    @GetMapping(value = "/getRoles",produces = "application/json;charset=UTF-8")
    public Result<List<String>> login(@RequestParam(value = "username",required = false) String username){
        return Result.of(Code.SC_OK.getState(),Code.SC_OK.getDescription(),sysUserService.findRolesByUserName(username));
    }
}
